//
//  QueryViewController.m
//  Supermarket
//
//  Created by qskj-ios on 2018/12/11.
//  Copyright © 2018年 com.qiansheng. All rights reserved.
//

#import "QueryViewController.h"

@interface QueryViewController ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;

- (IBAction)didClickedCloseButton:(id)sender;
- (IBAction)didClickedSureButton:(id)sender;

@end

@implementation QueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.type == 0) {
        self.titleLabel.text = @"输入兑换码";
        self.textField.placeholder = @"请输入兑换码";
    } else if (self.type == 1) {
        self.titleLabel.text = @"输入商品名称";
        self.textField.placeholder = @"请输入商品名称";
    } else if (self.type == 2) {
        self.titleLabel.text = @"输入书籍名称";
        self.textField.placeholder = @"请输入书籍名称";
    }
    
    [self.textField customBorderWithSize:kSeparatorHeight andPosition:UIViewBorderPositionBottom andColor:kSeparatorColor andLeadingInset:0 andTrailingInset:0];
}

- (IBAction)didClickedCloseButton:(id)sender {
    [self dismissPresentationControllerAnimated:YES completion:nil];
}

- (IBAction)didClickedSureButton:(id)sender {
    WEAKSELF
    if (self.type == 0) {
        if (self.textField.text.length == 0) {
            [SVProgressHUD showErrorWithStatus:@"请输入兑换码"];
            return;
        }
        
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setObject:UserInfo.id forKey:@"user_id"];
        [parameters setObject:self.offset_id forKey:@"id"];
        [parameters setObject:self.textField.text forKey:@"code"];
        [SVProgressHUD showWithStatus:@"正在请求...."];
        [NetworkHelper apiPost:@"manager/offset" parameters:parameters completion:^(id info, NSError *error) {
            [SVProgressHUD dismiss];
            if (!error) {
                NSString *remarks = [info objectForKey:@"remarks"];
                if ([[info objectForKey:@"sta"] integerValue] == 1) {
                    [SVProgressHUD showSuccessWithStatus:remarks];
                    [weakSelf dismissPresentationControllerAnimated:YES completion:nil];
                    if (weakSelf.querySuccessBlock) {
                        weakSelf.querySuccessBlock();
                    }
                } else {
                    [SVProgressHUD showErrorWithStatus:remarks];
                }
            }
        }];
        
    } else if (self.type == 1) {
        if (self.textField.text.length == 0) {
            [SVProgressHUD showErrorWithStatus:@"请输入商品名称"];
            return;
        }
        
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setObject:UserInfo.id forKey:@"user_id"];
        [parameters setObject:self.textField.text forKey:@"goods_name"];
        [SVProgressHUD showWithStatus:@"正在请求..."];
        [NetworkHelper apiPost:@"manager/creat_rent" parameters:parameters completion:^(id info, NSError *error) {
            [SVProgressHUD dismiss];
            if (!error) {
                NSString *remarks = [info objectForKey:@"remarks"];
                if ([[info objectForKey:@"sta"] integerValue] == 1) {
                    [SVProgressHUD showSuccessWithStatus:remarks];
                    [weakSelf dismissPresentationControllerAnimated:YES completion:nil];
                } else {
                    [SVProgressHUD showErrorWithStatus:remarks];
                }
            }
        }];
        
    } else if (self.type == 2) {
        if (self.textField.text.length == 0) {
            [SVProgressHUD showErrorWithStatus:@"请输入书籍名称"];
            return;
        }
        
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setObject:UserInfo.id forKey:@"user_id"];
        [parameters setObject:self.textField.text forKey:@"book_name"];
        [SVProgressHUD showWithStatus:@"租借中..."];
        [NetworkHelper apiPost:@"manager/creat_book" parameters:parameters completion:^(id info, NSError *error) {
            [SVProgressHUD dismiss];
            if (!error) {
                NSString *remarks = [info objectForKey:@"remarks"];
                if ([[info objectForKey:@"sta"] integerValue] == 1) {
                    [SVProgressHUD showSuccessWithStatus:remarks];
                    [weakSelf dismissPresentationControllerAnimated:YES completion:nil];
                } else {
                    [SVProgressHUD showErrorWithStatus:remarks];
                }
            }
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
